﻿<System.ComponentModel.TypeConverter(GetType(CLDS_Colour_Collection_Converter))> _
Public Class CLDS_Colour_Collection
    Inherits CollectionBase
    Default Public Property Item(ByVal index As Integer) As Color
        Get
            Return CType(List(index), Color)
        End Get
        Set(ByVal Value As Color)
            List(index) = Value
        End Set
    End Property
    Public Sub Add(ByVal p_colour As Color)
        List.Add(p_colour)
    End Sub
    Public Overrides Function Equals(ByVal val As Object) As Boolean
        If TypeOf (val) Is CLDS_Colour_Collection Then
            Dim ccObj As CLDS_Colour_Collection = CType(val, CLDS_Colour_Collection)
            If ccObj.ToString() = Me.ToString() Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function
    Public Function IndexOf(ByVal value As Color) As Integer
        Return List.IndexOf(value)
    End Function

    Public Sub Insert(ByVal index As Integer, ByVal value As Color)
        List.Insert(index, value)
    End Sub

    Public Sub Remove(ByVal value As Color)
        List.Remove(value)
    End Sub

    Public Function Contains(ByVal value As Color) As Boolean
        Return List.Contains(value)
    End Function

    Protected Overrides Sub OnInsert(ByVal index As Integer, ByVal value As Object)
        If Not value.GetType() Is GetType(Color) Then
            Throw New ArgumentException("Value must be of type Color.", "value")
        End If
    End Sub

    Protected Overrides Sub OnRemove(ByVal index As Integer, ByVal value As Object)
        If Not value.GetType() Is GetType(Color) Then
            Throw New ArgumentException("Value must be of type Color.", "value")
        End If
    End Sub

    Protected Overrides Sub OnSet(ByVal index As Integer, ByVal oldValue As Object, ByVal newValue As Object)
        If Not newValue.GetType() Is GetType(Color) Then
            Throw New ArgumentException("New value must be of type Color.", "newValue")
        End If
    End Sub

    Protected Overrides Sub OnValidate(ByVal value As Object)
        If Not value.GetType() Is GetType(Color) Then
            Throw New ArgumentException("Value must be of type Color.", "value")
        End If
    End Sub
    Public Sub FromString(ByVal txt As String)
        Me.List.Clear()
        Dim colours() As String = txt.Split(New Char() {";"})
        For Each srgb As String In colours
            Dim rgb() As String = srgb.Split(New Char() {","})
            Try
                Me.List.Add(Color.FromArgb(Val(rgb(0)), Val(rgb(1)), Val(rgb(2))))
            Catch
                Throw New InvalidCastException("Invalid Color serialization '" & srgb & "'")
            End Try
        Next srgb
    End Sub
    Public Sub New()
    End Sub
    Public Sub New(ByVal txt As String)
        FromString(txt)
    End Sub

    Public Overrides Function ToString() As String
        Dim txt As String = ""
        For Each p_colour As Color In MyBase.List
            txt &= ";" & Trim(Str$(p_colour.R)) & "," & Trim(Str$(p_colour.G)) & "," & Trim(Str$(p_colour.B))
        Next p_colour

        If txt.Length > 0 Then txt = txt.Substring(1)

        Return txt
    End Function
End Class
